package com.sskj.level.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.lib.bean.PageBean;
import com.sskj.level.bean.TradeListBean;
import com.sskj.level.http.HttpConfig;
import com.sskj.level.ui.fragment.EntrustFragment;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.List;

import io.reactivex.Flowable;


public class EntrustFragmentPresenter extends BasePresenter<EntrustFragment> {
    public Flowable<List<TradeListBean>> getTradeListFlow() {
        return httpService.getTradeList("1", null)
                .tag(this)
                .converter(new JsonConverter<HttpData<PageBean<TradeListBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList());

    }

    public void getTradeList() {
        httpService.getTradeList("1", null)
                .tag(this)
                .execute(new JsonCallBack<HttpData<PageBean<TradeListBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<PageBean<TradeListBean>>> response) {
                        HttpData<PageBean<TradeListBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateUI(httpData.getData().getList());
                        }
                    }
                });

    }

    public void cancelOrder(String orderId) {
        httpService.cancelOrder(orderId)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                           mView.cancelOrderSuccess();
                        }
                    }
                });
    }
}
